package com.dengc.controller;

import com.dengc.mapper.SysRoleMapper;
import com.dengc.pojo.SysRole;
import com.dengc.pojo.SysUser;
import com.dengc.service.SysRoleService;
import com.dengc.service.SysUserService;
import com.dengc.utils.Contants;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller
public class SysRoleController {

    @Autowired
    SysRoleService sysRoleService;
    @Autowired
    SysUserService sysUserService;

    //进入角色管理页面
    @RequestMapping("/togetroleAll/list")
    public String getAllSysRoleList(@RequestParam(value = "queryRealName",required = false) String realName,
                                    @RequestParam(value = "queryRoleId",required = false) Integer roleId,
                                    @RequestParam(value = "pageIndex",required = false) Integer pageIndex,
                                    HttpServletRequest request){
        //用户
        SysRole sysRoleCondition = new SysRole();
        if(realName!=null && !"".equals(realName)){
            sysRoleCondition.setName(realName);
            //回显模糊查询数据
            request.setAttribute("queryRealName",realName);
        }
        if (roleId!=null && !"".equals(roleId)){
            sysRoleCondition.setId(roleId);
            //回显roleid
            request.setAttribute("queryRoleId",roleId);
        }
        int pageNum = 1;//默认设置为第一页
        if (pageIndex!=null){
            pageNum = pageIndex;
        }
        PageInfo<SysRole> pageInfo = sysRoleService.getSysRoleByPage(sysRoleCondition, pageNum);
        request.setAttribute("pageInfo",pageInfo);
        return "sysRole/list";
    }

    //角色管理界面修改功能1.进入修改页面
    @RequestMapping("/SysRoleUpdate/{id}")
    public String upDateRole1(@PathVariable("id") Integer id, HttpServletRequest request){
        SysRole sysRole = sysRoleService.getIdSysRole(id);
        request.setAttribute("sysRole",sysRole);
        return "sysRole/update";
    }

    //角色管理界面修改功能2.修改信息
    @RequestMapping("/sys/role/update")
    @ResponseBody
    public String upDateRole2(SysRole sysRole,HttpServletRequest request){
        int num =  sysRoleService.upDateRole(sysRole);
        if(num>0){
            //修改成功
            return "<script>alert('修改成功');location.href='"+request.getContextPath()+"/togetroleAll/list'</script>";
        }else{
            //修改失败
            return "<script>alert('修改失败');location.href='"+request.getContextPath()+"/togetroleAll/list'</script>";
        }
    }

    //角色信息界面删除用户信息功能
    @RequestMapping("/SysRoleDelete/{id}")
    @ResponseBody
    public String SysRoleDelete(@PathVariable("id") Integer id,
                                HttpServletRequest request){
        if (sysUserService.getCountByRoleId(id)>0){
            return "<script>alert('该角色还存在用户信息不能进行删除！');location.href='"+request.getContextPath()+"/togetroleAll/list'</script>";
        }
        int num = sysRoleService.deleteRole(id);
        if(num>0){
            //删除成功
            return "<script>alert('删除成功');location.href='"+request.getContextPath()+"/togetroleAll/list'</script>";
        }else{
            //删除失败
            return "<script>alert('删除失败');location.href='"+request.getContextPath()+"/togetroleAll/list'</script>";
        }
    }


    //角色信息界面添加用户信息功能 1.进入添加页面
    @RequestMapping("/SysRoleToAdd")
    public String SysRoleToAdd1(){
        return "sysRole/add";
    }

    //用户信息界面添加用户信息功能 2.添加信息
    @RequestMapping("/sys/role/add")
    @ResponseBody
    public String SysRoleToAdd2(SysRole sysRole, HttpServletRequest request, HttpSession session){
        SysUser user = (SysUser) session.getAttribute(Contants.USERSESSION);
        sysRole.setCreateTime(new Date());
        sysRole.setCreatedUserId(user.getId());
        int num =  sysRoleService.addRole(sysRole);
        if(num>0){
            //保存成功
            return "<script>alert('新增成功');location.href='"+request.getContextPath()+"/togetroleAll/list'</script>";
        }else{
            //保存失败
            return "<script>alert('新增失败');location.href='"+request.getContextPath()+"/togetroleAll/list'</script>";
        }
    }

}
